package com.cn.qgx428.lazydiary.ui.fragment


import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView

import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.SPUtil
import com.cn.qgx428.lazydiary.anim.viewPager.DepthPageTransformer
import com.cn.qgx428.lazydiary.ui.activity.HomeActivity
import kotlinx.android.synthetic.main.fragment_install.*
import org.jetbrains.anko.startActivity


/**
 * A simple [Fragment] subclass.
 */
class InstallFragment : BaseFragment() {


    val imgsId = arrayOf(R.drawable.guide1, R.drawable.guide2, R.drawable.guide3)

    override fun getLayoutId() = R.layout.fragment_install

    override fun initView() {
        installF_viewPager.adapter = mViewPagerAdapet()
        installF_viewPager.setPageTransformer(true, DepthPageTransformer())
        installF_viewPager.setCurrentItem(0)
        installF_indicator.setViewPager(installF_viewPager)

        //活动完所有页面显示按钮
        installF_viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                if (position == imgsId.size-1){
                    install_exBtn.visibility = View.VISIBLE
                }
            }

            override fun onPageSelected(position: Int) {

            }
        })

        //监听记录用户已不是第一次使用此APP
        install_exBtn.setOnClickListener {
            context.startActivity<HomeActivity>()
            SPUtil.userInstalledApp()
            activity.finish()
        }
    }


    inner class mViewPagerAdapet() : PagerAdapter(){
        override fun isViewFromObject(view: View?, `object`: Any?): Boolean{
            return  view == `object`
        }

        override fun getCount() = imgsId.size

        override fun instantiateItem(container: ViewGroup?, position: Int): Any {
            //创建ImgView
            val imgView = ImageView(context)
            imgView.setImageResource(imgsId[position])
            val params : FrameLayout.LayoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT)
            imgView.layoutParams = params
            imgView.scaleType = ImageView.ScaleType.FIT_XY
            container!!.addView(imgView)
            log(imgsId[position].toString())

            return imgView
        }

        override fun destroyItem(container: ViewGroup?, position: Int, `object`: Any?) {
            container?.removeView(`object` as View?)
        }
    }

}
